/**
 * Copyright 2021 xpstem.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xpstem.lelink.command;

import com.xpstem.lelink.enums.CommandCode;
import com.xpstem.lelink.enums.CommandType;
import com.xpstem.lelink.enums.ReplyStatus;
import com.xpstem.lelink.enums.ReplyType;
import com.xpstem.lelink.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * EV3 system command base class.
 * @author billy zhang
 */
public abstract class EV3SystemCommand implements EV3Command {

    private static final Logger log = LoggerFactory.getLogger(EV3SystemCommand.class);

    private ReplyType replyType;

    public abstract CommandCode getCommandCode();

    protected byte[] getPayload() throws IOException {
        return new byte[0];
    }

    @Override
    public CommandType getType() {
        return CommandType.SYSTEM_COMMAND_REPLY;
    }

    @Override
    public EV3CommandPackage getMessage() throws IOException {

        EV3SystemCommandPackage pkg = new EV3SystemCommandPackage(
                getType(), getCommandCode(), getPayload());

        return pkg;

    }

    @Override
    public void response(EV3ReplyPackage replyPackage) throws IOException {

        InputStream inputStream = new ByteArrayInputStream(replyPackage.getRawData());
        //skip counter bytes.
        inputStream.skip(2);
        //byte4:
        replyType = ReplyType.ofValue((short)inputStream.read());
        replyPackage.setType( replyType );
        //byte5:
        replyPackage.setCode( CommandCode.ofValue((short)inputStream.read()) );
        //byte6:
        replyPackage.setStatus( ReplyStatus.ofValue((short)inputStream.read()) );

        if (!replyPackage.success()) {
            return;
        }

        //byte7 - n:
        int len = inputStream.available();
        byte[] payload = new byte[len];
        inputStream.read(payload);

        String hexStr = StrUtil.toHexWithSep(payload, "_");
        log.info("payload: {}", hexStr);
        replyPackage.setPayload(payload);

        doResponse(replyPackage);
    }

    protected void doResponse(EV3ReplyPackage reply) {

    }

    @Override
    public ReplyType getReplyType() {
        return replyType;
    }

    @Override
    public String description() {
        return getClass().getSimpleName();
    }
}
